// pages/Main/mainIndex/mainIndex.js
var businessLogic = require('../../../utils/NetWork/Model/MainbusinessLogic.js')
var common = require('../../../utils/common.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: [],
    userid: "",
    orgid: "",
    ryid: "",
    newdata: [],
    num: 1,
    scrollHeight: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let c = common.Getorgid();
    var scrollHeight = scrollHeight;
    wx.getSystemInfo({
      success: function (res) {
        scrollHeight = res.windowHeight
      }
    });
    this.setData({
      orgid: c.orgid,
      userid: c.userid,
      ryid: options.id,
      scrollHeight: scrollHeight,
    });
    this.list();
    console.log(this.data.ryid)
  },
  bindDownLoad: function () {
    if (this.data.lenpage > this.data.num) {
      var di = this.data.num;
      var di = di + 1;
      this.setData({
        num: di
      });
      this.list();
    } else {
      common.showToast("到底了", "loading");
    }
  },
  

  //8.12.1
  list: function () {
    // 第二步 传参
    common.showToast("加载中", "loading");
    let params = { "page": this.data.num, "num": 1000, "userid": this.data.userid, "orgid": this.data.orgid, "searchField": "peopleid,testid", "searchValue": "" + this.data.userid + "," + this.data.ryid + "" }
    //第三步 调用接口
    businessLogic.AaccessTo(params, (res) => {
      for (var i = 0; i < res.length; i++) {
        if (res[i].dotime == "" || res[i].dotime == undefined) {
          res[i].dotime = '暂无数据';
        } else {
          res[i].dotime = common.formatSeconds(res[i].dotime);
        }
      }
      for (var a = 0; a < res.length; a++) {
        if (res[a].newstime == "" || res[a].newstime == undefined) {
          res[a].newstimemiao = '暂无数据';
        } else {
          res[a].newstimemiao = common.toDate2(res[a].newstime);
        }
        if (res[a].newstime == "" || res[a].newstime == undefined) {
          res[a].newstimeday = '暂无数据';
        } else {
          res[a].newstimeday = common.toDate(res[a].newstime);
        }

      }
      console.log(res);
      var newtimeday = [];
      var timearray = [];
      for (var i = 0; i < res.length; i++) {
        newtimeday[i] = res[i].newstimeday;
        if (timearray.indexOf(newtimeday[i]) > -1) {
          
        }else{
          if (timearray.length==0){
            timearray[0] = newtimeday[i] == null || newtimeday[i] == undefined || newtimeday[i] == "" ? "" : newtimeday[i];
          }else{
            timearray[timearray.length] = newtimeday[i] == null ? "" : newtimeday[i] ;
          }
        }
      }
      //console.log(timearray);
      this.setData({
        time: timearray
      })
      let temp = new Array;
      for (var e = 0; e < timearray.length; e++){
        let obj = {};
        let aa = 0;
        for (var f = 0; f < newtimeday.length; f++){
          if (timearray[e].indexOf(newtimeday[f]) > -1) {
            obj[aa] = res[f];
            aa +=1;
            if (f == (newtimeday.length-1)){
              temp[e] = obj;
            }
            
          } else {
            if (obj !== {} || obj !== ""){
                temp[e] = obj;
              }
          }
        }
      }
      this.setData({
        content: temp
      })
    
    });

  },


  teacher: function () {
    wx.navigateTo({
      url: "../teacherOrder/teacherOrder"
    });
  }, parents: function () {
    wx.navigateTo({
      url: "../post/post"
    });
  }, student: function () {
    wx.navigateTo({
      url: "../post/post"
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },



})